# -*- coding:UTF-8 -*-
# 接口请求签名生成
import time
import os
import config as config
import convertTo as convertTo


class signature(object):
    def __init__(self):
        # 获取当前文件的path
        filePath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        # print(filePath)
        self.confPath = filePath + "/data/conf.ini"
        self.cr = config.ConfigReader(self.confPath)


    # 请求时间写入配置文件
    def r_time(self):
        r_time = time.time()
        rtime = str(int(r_time))
        self.cr.set('Andorid_Basic', 'requestTime', rtime)
        self.rtime = self.cr.get('Andorid_Basic', 'requestTime')
        return self.rtime


    # 请求参数按照首字母顺序排列并组合
    def sortParam(self, paramStr):
        # paramStr = {"areaCode":"86","loginType":"Xiaomi","password":"HPX7jHFSajLgcFwy2xUgbg==","username":"17177770008"}
        # 将请求参数按照首字母顺序排列
        dic = sorted(paramStr.items(), key=lambda x: x[0])
        parameterDic= dict(dic)
        parametersNew = []
        # print(parameterDic.keys())
        # 将请求参数按照key = value & key = value组合
        for i in parameterDic.keys():
            parameterValue = parameterDic[i]
            # print(parameterValue)
            parameterNew = "\"%s\":\"%s\"" % (i, parameterValue)
            parametersNew.append(parameterNew)
            # print(parameterNew)
        self.newParam = ",".join(str(i) for i in parametersNew)
        self.newParam = "{%s}" % self.newParam
        # print(newPara)
        return self.newParam


    # 生成ios签名
    def androidSignature(self, url_path, paramStr1=None):
        s = signature()
        url = self.cr.get('Andorid_Basic', 'url')
        t_url = url + url_path
        paramStr1 = s.sortParam(paramStr1)
        apiVersion = self.cr.get('Andorid_Basic', 'apiversion')
        appVersion = self.cr.get('Andorid_Basic', "appversion")
        token = self.cr.get('Andorid_Basic', "token")
        # deviceCode = self.cr.get('Andorid_Basic', 'deviceCode')
        deviceCode = ''
        userId = self.cr.get('Andorid_Basic', 'userId')
        r_time = s.r_time()
        signatureStr = "=" + token + " " + "[" + " " + ";" + apiVersion + " " + "<" + userId + "_" + " " + "%" + " " + r_time + "*" + " " + "!" + appVersion + "……" + t_url + " " + "+" + paramStr1 + " " + "]" + "-" + deviceCode + "6"
        # print(signatureStr)
        # 将组合的签名字符串进行MD5加密
        ct = convertTo.Convert(signatureStr)
        androidSign = ct.convertMD5()
        print(androidSign)
        return androidSign


    def iosSignature(self, url_path, paramStr1):
        s = signature()
        url = self.cr.get('iOS_Basic', 'url')
        t_url = url + url_path
        paramStr1 = s.sortParam(paramStr1)
        apiVersion = self.cr.get('iOS_Basic', 'apiversion')
        appVersion = self.cr.get('iOS_Basic', "appversion")
        token = self.cr.get('iOS_Basic', "token")
        # deviceCode = self.cr.get('iOS_Basic', 'deviceCode')
        deviceCode = ''
        userId = self.cr.get('iOS_Basic', 'userId')
        r_time = s.r_time()
        signatureStr = paramStr1 + " " + "%" + t_url + " " + "|" + apiVersion + " " + "?" + "" + "}" + appVersion + " " + "%" + r_time + " " + userId + "[" + token
        # print(signatureStr)
        # 将组合的签名字符串进行MD5加密
        ct = convertTo.Convert(signatureStr)
        self.iosSign = ct.convertMD5()
        print(self.iosSign)
        return self.iosSign

# 测试数据
# paramstr = {"areaCode":"86","loginType":"Xiaomi","password":"HPX7jHFSajLgcFwy2xUgbg==","username":"17177770008"}
# url_path = "/v2/user/login"
# s = signature()
# s.iosSignature(url_path, paramstr)
